/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui.slidebox;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
	
/**
 * UI for a button used in the toolbar, normaly with an icon.
 * 
 * @version 1.0 
 * @author Dimitrij Pankratz, Anton Musichin
 */
public class CloseButtonSlideBoxUI extends BasicButtonUI {

	/**
	 * Icon of the button, while mouse is over.
	 */
	protected ImageIcon hoverIcon;
	
	protected ImageIcon normalIcon;
	
	/**
	 * 
	 */
	protected boolean mouseOver = false;
	
	/**
	 * 
	 */
	protected boolean mousePressed = false;
	
	/**
	 * 
	 */
	protected MouseAdapter mouseListener = null;
	
	
	/**
	 * @param hoverIcon
	 */
	public CloseButtonSlideBoxUI() {
		super();
		this.normalIcon = new ImageIcon(
				getClass().getResource("images/close_btn_normal.png"));
		this.hoverIcon = new ImageIcon(
				getClass().getResource("images/close_btn_hover.png"));
	}
	

	protected void installDefaults(AbstractButton b)
	{
		super.installDefaults(b);

		b.setBorder(new EmptyBorder(0, 0, 0, 0));
	}


	public void paint(Graphics g, JComponent c)
	{
		JButton buttonComponent = (JButton) c;
		if (mouseListener == null) {
			createMouseLister();
			buttonComponent.addMouseListener(mouseListener);
		}
		
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
				RenderingHints.VALUE_ANTIALIAS_ON);
		
		if (mouseOver) {
			Icon hoverIcon = this.hoverIcon;
			if (hoverIcon != null) {
					hoverIcon.paintIcon(buttonComponent, g, 0, 0);
			}
		} else {
			Icon normalIcon = this.normalIcon;
			if (normalIcon != null) {
				normalIcon.paintIcon(buttonComponent, g, 0, 0);
			}
		}
	}

	
	/**
	 * 
	 */
	private void createMouseLister() {
		mouseListener = new MouseAdapter() {
		      public void mouseEntered(MouseEvent mouseEvent) {
		    	  mouseOver = true;
		    	  if (mouseEvent.getComponent() != null)
		    		  mouseEvent.getComponent().repaint();
		      }
		      
		      public void mouseExited(MouseEvent mouseEvent) {
		    	  mouseOver = false;
		    	  if (mouseEvent.getComponent() != null)
		    		  mouseEvent.getComponent().repaint();
		      }
		      
		      public void mousePressed(MouseEvent mouseEvent) {
		    	  mousePressed = true;
		    	  if (mouseEvent.getComponent() != null)
		    		  mouseEvent.getComponent().repaint();
		      }
		      
		      public void mouseReleased(MouseEvent mouseEvent) {
		    	  mousePressed = false;
		    	  if (mouseEvent.getComponent() != null)
		    		  mouseEvent.getComponent().repaint();
		      }
		};
	}
}